package com.hmys.api.controller;

import cn.jsms.api.JSMSClient;
import cn.jsms.api.SendSMSResult;
import cn.jsms.api.common.model.SMSPayload;
import com.hmys.api.config.SMSConfig;
import com.hmys.api.response.Result;
import com.hmys.api.response.ResultCode;
import com.hmys.api.service.WebUserService;
import com.hmys.api.utils.ValidationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chen365 on 2017/7/17.
 */

@RestController
@Api(tags = "短信")
public class SMSController {

    @Autowired
    private SMSConfig smsConfig;

    @Autowired
    private WebUserService webUserService;


    /**
     * 获取验证短信
     *
     * @param phone
     * @return
     */
    @ApiOperation(value = "获取验证短信", notes = "获取验证短信")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "type", value = "短信类型（login,register,message,apply,find）", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "phone", value = "手机号", required = true, paramType = "path")
    })
    @GetMapping(value = "/sms/{type}/{phone}")
    public Object getSMS(@PathVariable String type, @PathVariable String phone) {
        try {
            if (!ValidationUtil.isPhone(phone)) {
                return new Result(ResultCode.ERROR_PHONE);
            }
            if (type.equals("login")) {
                if (!webUserService.isExist(phone)) {
                    return new Result(ResultCode.REGISTERED_PHONE);
                }
            } else if (type.equals("register")) {
                if (webUserService.isExist(phone)) {
                    return new Result(ResultCode.PHONE_REGISTERED);
                }
            } else if (type.equals("message")) {

            } else if (type.equals("apply")) {

            } else if (type.equals("find")) {

            } else {

            }
            JSMSClient jsmsClient = new JSMSClient(smsConfig.getSecret(), smsConfig.getAppkey());
            SMSPayload payload = SMSPayload.newBuilder().setMobileNumber(phone).setTTL(300).setTempId(157640).build();
            SendSMSResult result = jsmsClient.sendSMSCode(payload);
            if (result.isResultOK()) {
                return new Result(ResultCode.SUCCESS, result.getMessageId().toString());
            } else {
                return new Result(ResultCode.FAIL, result.getOriginalContent());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.ERROR);
        }
    }

//    /**
//     * 获取验证短信
//     *
//     * @param phone
//     * @return
//     */
//    @ApiOperation(value = "获取验证短信", notes = "获取验证短信")
//    @ApiImplicitParams({
//            @ApiImplicitParam(dataType = "String", name = "phone", value = "手机号", required = true, paramType = "path")
//    })
//    @GetMapping(value = "/sms/{phone}")
//    public Object sendSMS(@PathVariable String phone) {
//        try {
//            if (!ValidationUtil.isPhone(phone)) {
//                return new Result(ResultCode.ERROR_PHONE);
//            }
//            JSMSClient jsmsClient = new JSMSClient(smsConfig.getSecret(), smsConfig.getAppkey());
//            SMSPayload payload = SMSPayload.newBuilder().setMobileNumber(phone).setTTL(300).setTempId(157640).build();
//            SendSMSResult result = jsmsClient.sendSMSCode(payload);
//            if (result.isResultOK()) {
//                return new Result(ResultCode.SUCCESS, result.getMessageId().toString());
//            } else {
//                return new Result(ResultCode.FAIL, result.getOriginalContent());
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(ResultCode.ERROR);
//        }
//    }

}
